package acm.同步赛4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class G {
    public static void main(String[] args) throws IOException {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        String[] read=null;
        read=in.readLine().split(" ");
        int n = Integer.parseInt(read[0]);
        int m = Integer.parseInt(read[1]);
        Map<String,String>map=new HashMap<>();
        while (n-- > 0) {
            read=in.readLine().split(" ");
            map.put(read[0],read[1]);
            map.put(read[1],read[0]);
        }
        while(m-->0){
            System.out.println(map.getOrDefault(in.readLine(),"sdmrlh"));
        }
    }
}
